import { Vec3 } from 'cc';
import { ViewUtil } from 'db://app-game/core/utils/ViewUtil';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { RectilinearMoveComp } from '../../../base/bll/move/RectilinearMoveSystem';
import { smc } from '../../../singleton/SingletonModuleComp';
import { EffectsCollisionComp } from '../bll/EffectsCollisionSystem';
import { Effects } from '../Effects';
import { EffectsViewComp } from '../view/EffectsViewComp';

/** 弓箭特效 */
@ecs.register('BowEffects')
export class BowEffects extends Effects {
  // 业务层
  RectilinearMove!: RectilinearMoveComp; // 直线移动
  EffectsCollision!: EffectsCollisionComp; // 碰撞检测

  // 视图层
  EffectsView!: EffectsViewComp; // 特效视图
  /** 加载特效视图 */
  load(params: { id: number; pos: Vec3; direction: Vec3; speedOfMove: number; moveTime: number }) {
    const { id, pos, direction, speedOfMove, moveTime } = params;
    this.EffectsModel.id = id;

    // 1.加载特效视图
    let node = ViewUtil.createPrefabNode(this.EffectsModel.prefabPath);
    const mv = node.addComponent(EffectsViewComp);
    mv.node.setPosition(pos);
    this.add(mv);
    node.parent = smc.level.LevelMap.effectFactoryNode;

    // 2.添加移动组件
    this.addRectilinearMove(direction, speedOfMove, moveTime);

    // 3.添加碰撞检测
    this.add(EffectsCollisionComp);
    this.EffectsCollision.node = this.EffectsView.node;
    this.EffectsCollision.colliderType = 'Rectangle';
    this.EffectsCollision.onCollision = this.onCollision;
  }

  /**
   * 添加直线移动组件
   * @param velocity 移动方向
   * @param speedOfMove 移动速度
   * @param moveTime 移动时间
   */
  addRectilinearMove(velocity: Vec3, speedOfMove: number, moveTime: number) {
    const rm = this.add(RectilinearMoveComp);
    rm.node = this.EffectsView.node;
    rm.velocity = velocity;
    rm.speedOfMove = speedOfMove;
    rm.moveTime = moveTime;
    rm.onComplete = () => {
      this.destroy();
    };
  }

  /** 碰撞回调 */
  onCollision(colliderList: any) {
    console.log(colliderList);
  }
}
